// packageI/AdBright/AdBrightIndex/AdBrightIndex.js
const app = getApp() // 获取小程序全局对象
try {

  Page({
    /**
     * 页面的初始数据
     */
    data: {
      integral: '',
      placementId: null,
      mini_form_id: null, //小程序流量主广告单元ID
      level_credit: 0, //当前等级观看可获取积分
      level_data: [],
      adv_list: [],
      member: {},
      level_index: 0,
      is_show:0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.getData();
      const value = wx.getStorageSync('integral');
      if (value) {
        this.setData({
          integral: value
        });
      }
    },
    getADData(e) {
      if(this.data.adv_info.adv_type == 2){
        wx.navigateTo({url: '/packageI/AdBright/AdBrightWx/AdBrightWx?wxad=' + this.data.mini_form_id})
        return
      }
      this.setData({
        placementId: e.target.dataset.id
      })
      
      if (!app.adConfig.app_id || !app.adConfig.domain) {
        wx.showToast({
          icon: "none",
          title: '缺失广告配置信息',
          duration: 1000,
        });
        return
      }
      wx.navigateTo({url: '/packageI/AdBright/AdBrightPage/AdBrightPage?adv_id='+ this.data.placementId +'&wxad=' + this.data.mini_form_id})
      return
    },

    toPoint() {
      wx.navigateTo({
        url: '/packageI/AdBright/adBrightPoint/adBrightPoint'
      })
    },
    back() {
      try {
        wx.navigateBack({
          delta: 1,
          fail() {
            wx.reLaunch({
              url: "/packageG/index/index"
            });
          },
        });
      } catch (error) {
        wx.reLaunch({
          url: "/packageG/index/index"
        });
      }
    },
    getData() {
      let urlStr = app.getNetAddresss("plugin.flyers-advertise.frontend.mini.index");
      app._postNetWork({
        url: urlStr,
        data: {
          validate_page:1
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            let level_credit;
            res.data.level_data.forEach(element => {
              if (element.level_id == res.data.member.level_id) {
                level_credit = element.amount
              }
            });
            let level_index = 0;
            res.data.level_data.forEach((item,index)=>{
              if(item.level_id ==  res.data.member.level_id){
                level_index = index;
              }
            })
            this.setData({
              is_show:res.data.is_show,
              mini_form_id:res.data.mini_form_id || '',
              level_index: level_index,
              level_data: res.data.level_data,
              adv_list: res.data.adv_list,
              adv_info: res.data,
              member: res.data.member,
              level_credit: level_credit
            })
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1000,
            });
          }
        },
        fail: (res) => {
          console.log(res);
        },
      });

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
  })

} catch (error) {
  // wx.showModal({
  //   title: '提示',
  //   content: '广告加载失败，请检查配置',
  //   showCancel: false,
  //   success(res) {
  //     if (res.confirm) {
  //       wx.reLaunch({
  //         url: "/packageG/index/index"
  //       });
  //     }
  //   }
  // })
}